﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using LPV_POS.Baocao;

namespace LPV_POS.mh_pos.mh_inbaocao
{
    class mh_in_KIM
    {
        
        //tbBH_PHIEU_TMP.WriteXmlSchema(Application.StartupPath + "//BH_PHIEU_TMP.XSD");

        public void f_inhoadon(System.Data.DataTable _tbBH_PHIEU_TMP, System.Data.DataTable __tbBH_PHIEU_CT_TMP, Boolean _iInLai, string sTieude)
        {
            System.Data.DataTable _tbBH_PHIEU_CT_TMP = __tbBH_PHIEU_CT_TMP.Copy();
            System.Drawing.Printing.PrinterSettings cls = new System.Drawing.Printing.PrinterSettings();
            string sPrinterDefault = cls.PrinterName;
            try
            {
                //_tbBH_PHIEU_CT_TMP.TableName = "BH_PHIEU_CT_TMP";
                //_tbBH_PHIEU_CT_TMP.WriteXmlSchema(Application.StartupPath + "\\BH_PHIEU_CT_TMP.XSD");
                
                DataTable tbHinh = this.Create_Datatable();
                tbHinh.TableName = "dt_hinh";
                ////tbHinh.WriteXmlSchema(Application.StartupPath + "\\dt_hinh.XSD");

                LPV_POS.Baocao.Bill_IN_KIM rptIN_KIM = new LPV_POS.Baocao.Bill_IN_KIM();

                //LPV_POS.Baocao.Bill_A5 rptA5tmp = new LPV_POS.Baocao.Bill_A5();
                //ReportDocument rptIN_KIM = new ReportDocument();
                //string spath = Application.StartupPath + "\\..\\..\\Baocao\\Bill_A5.rpt";
                //rptIN_KIM.Load(spath);

                //
                int ii = 1;
                float iDISC1PT = 0;
                float iDISC1TI = 0;
                float iDISC1TI_All = 0;

                float iDISC2PT = 0;
                float iDISC2TI = 0;
                float iDISC2TI_All = 0;

                rptIN_KIM.DataDefinition.FormulaFields["tieude"].Text = "'" + sTieude + "'";

                for (int i = 0; i < _tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio'").Length; i++)
                {
                    if (i >= 0 && i <= 2)
                    {
                        float fSL = 0;
                        float fDG = 0;
                        float fTT = 0;

                        float.TryParse(_tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio'")[i]["CHIETKHAU_PHANTRAM"] + "", out iDISC1PT);
                        float.TryParse(_tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio'")[i]["CHIETKHAU_TIEN"] + "", out iDISC1TI);
                        iDISC1TI_All += iDISC1TI;
                        if (iDISC1PT > 0)
                            rptIN_KIM.DataDefinition.FormulaFields["DISC1PT"].Text = "'" + iDISC1PT.ToString("N0") + "%'";

                        float.TryParse(_tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio'")[i]["SO_LUONG"] + "", out fSL);
                        float.TryParse(_tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio'")[i]["DON_GIA"] + "", out fDG);
                        float.TryParse(_tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio'")[i]["THANH_TIEN"] + "", out fTT);

                        rptIN_KIM.DataDefinition.FormulaFields["SL_K" + (ii)].Text = "'" + fSL + "'";
                        rptIN_KIM.DataDefinition.FormulaFields["DG_K" + (ii)].Text = "'" + fDG.ToString("N0") + "'";
                        rptIN_KIM.DataDefinition.FormulaFields["TT_K" + (ii++)].Text = "'" + fTT.ToString("N0") + "'";
                        //
                        _tbBH_PHIEU_CT_TMP.Rows.Remove(_tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio'")[i--]);
                    }
                }
                rptIN_KIM.DataDefinition.FormulaFields["DISC1TI"].Text = "'" + iDISC1TI_All.ToString("N0") + "'";

                //
                foreach (DataRow dr in _tbBH_PHIEU_CT_TMP.Rows)
                {
                    if (dr["barcode"] + "" == "")
                        dr["barcode"] = dr["MAHH"];
                    /////////PHAN TRAM
                    float.TryParse(dr["CHIETKHAU_PHANTRAM"] + "", out iDISC2PT);
                    if (_tbBH_PHIEU_CT_TMP.Select("CHIETKHAU_PHANTRAM = " + iDISC2PT + "").Length == _tbBH_PHIEU_CT_TMP.Rows.Count)
                        rptIN_KIM.DataDefinition.FormulaFields["DISC2PT"].Text = "'" + iDISC2PT.ToString("N0") + "%'";

                    //////////TIEN
                    float.TryParse(dr["CHIETKHAU_TIEN"] + "", out iDISC2TI);
                    iDISC2TI_All += iDISC2TI;
                }
                
                /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                rptIN_KIM.Database.Tables["BH_PHIEU_CT_TMP"].SetDataSource(_tbBH_PHIEU_CT_TMP);
                rptIN_KIM.Database.Tables["BH_PHIEU_TMP"].SetDataSource(_tbBH_PHIEU_TMP);
                rptIN_KIM.Database.Tables["dt_hinh"].SetDataSource(tbHinh);
                /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                rptIN_KIM.DataDefinition.FormulaFields["DISC2TI"].Text = "'" + iDISC2TI_All.ToString("N0") + "'";
                //
                rptIN_KIM.DataDefinition.FormulaFields["DISC3TI"].Text = "'" + (iDISC1TI_All + iDISC2TI_All).ToString("N0") + "'";
                ///////////////////////////////////
                thuvien othuvien = new thuvien();
                //
                System.Data.DataTable tbDM_KHACHHANG = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@tre_tien_do_bi_MrLong_la" }, new object[] { "SELECT * FROM DM_KHACHHANG WHERE THEKHACHHANG = '" + _tbBH_PHIEU_TMP.Rows[0]["CUSTOMER_ID"] + "'" });
                if (tbDM_KHACHHANG.Rows.Count > 0)
                {
                    rptIN_KIM.DataDefinition.FormulaFields["KH_TEN"].Text = "'" + tbDM_KHACHHANG.Rows[0]["OBJNAME"].ToString().Trim() + "'";
                    rptIN_KIM.DataDefinition.FormulaFields["KH_DIACHI"].Text = "'" + tbDM_KHACHHANG.Rows[0]["DIACHI"].ToString().Trim() + "'";
                    rptIN_KIM.DataDefinition.FormulaFields["KH_MASOTHUE"].Text = "'" + tbDM_KHACHHANG.Rows[0]["MA_SO_THUE"].ToString().Trim() + "'";
                    rptIN_KIM.DataDefinition.FormulaFields["KH_NGUOILIENHE"].Text = "'" + tbDM_KHACHHANG.Rows[0]["NGUOILIENHE"].ToString().Trim() + "'";
                }
                else
                {
                    rptIN_KIM.DataDefinition.FormulaFields["KH_TEN"].Text = "''";
                    rptIN_KIM.DataDefinition.FormulaFields["KH_DIACHI"].Text = "''";
                    rptIN_KIM.DataDefinition.FormulaFields["KH_MASOTHUE"].Text = "''";
                    rptIN_KIM.DataDefinition.FormulaFields["KH_NGUOILIENHE"].Text = "''";
                }
                //
                System.Data.DataTable tbDM_COMPANY = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@gan_toi_0209_ma_chua_co_lich_nghile" }, new object[] { "SELECT * FROM DM_COMPANY" });
                rptIN_KIM.DataDefinition.FormulaFields["CTY_TEN"].Text = "'" + tbDM_COMPANY.Rows[0]["TENCONGTY"] + "'";
                rptIN_KIM.DataDefinition.FormulaFields["CTY_DIACHI"].Text = "'" + tbDM_COMPANY.Rows[0]["DIACHI"] + "'";
                rptIN_KIM.DataDefinition.FormulaFields["CTY_DIENTHOAI"].Text = "'" + tbDM_COMPANY.Rows[0]["DIENTHOAI"] + "'";

                string sTien_Chu = new cpublic().ChuyenSo_ThanhChu(_tbBH_PHIEU_TMP.Rows[0]["TONG_TIEN"] + "");
                if (sTien_Chu != "")
                    sTien_Chu = sTien_Chu.Trim() + " đồng chẵn";
                rptIN_KIM.DataDefinition.FormulaFields["tong_tien_bang_chu"].Text = "'" + sTien_Chu + "'";

                #region "   sPrinterDefault"
                rptIN_KIM.PrintOptions.PrinterName = sPrinterDefault;
                #endregion

                try
                {
                    if (new cpublic().Registry_Get("chkCrystalReport_Display") + "" == "True")
                    {
                        mh_View frm = new mh_View(rptIN_KIM);
                        frm.Show();
                    }
                }
                catch (Exception ex)
                {
                }
                
                rptIN_KIM.PrintToPrinter(0, true, 1, 100);
                if (Variable.tbBH_CAU_HINH.Rows[0]["MO_KET"] + "" == "True")
                    LPV_CASH_DRAWER.OpenCashDrawer(rptIN_KIM.PrintOptions.PrinterName);
                
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                MessageBox.Show(sPrinterDefault + "\n" + ex.Message, "");
            }
        }

        public void f_inhoadon(string _sBAN_MA, string _sPhieu_Ma, int _iDM_BAN_DAT_BAN_ID, Boolean _iInLai,string sTieude)
        {
            thuvien othuvien = new thuvien();
            int iflag = 4;
            if (_iInLai)
                iflag += 20;

            System.Data.DataTable _tbBH_PHIEU_TMP = othuvien.SP_SelectDataTable("sp_mh_banhang",
                                                        new string[] { "@flag", "@DM_BAN_MA", "@BH_PHIEU_MA", "@DM_BAN_DAT_BAN_ID" },
                                                        new object[] { iflag, _sBAN_MA, _sPhieu_Ma, _iDM_BAN_DAT_BAN_ID });

            System.Data.DataTable _tbBH_PHIEU_CT_TMP = othuvien.SP_SelectDataTable("sp_mh_banhang",
                                                        new string[] { "@flag", "@BH_PHIEU_MA", "@DM_BAN_DAT_BAN_ID", "@DM_BAN_MA" },
                                                        new object[] { iflag + 1, _sPhieu_Ma, _iDM_BAN_DAT_BAN_ID, _sBAN_MA });

            this.f_inhoadon(_tbBH_PHIEU_TMP, _tbBH_PHIEU_CT_TMP, _iInLai, sTieude);

        }

        private DataTable Create_Datatable()
        {
            DataTable dt = new DataTable();
            DataRow row;
            dt.Columns.Add("Hinh", System.Type.GetType("System.Byte[]"));
            //dt.Columns.Add("HinhDetail", System.Type.GetType("System.Byte[]"));
            row = dt.NewRow();

            //Create Filestream to read image
            FileStream fs;

            //Create variable BinaryReader to read bytes of image
            BinaryReader br;

            //Check exists of image

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "logo.bmp"))
            {
                //open image in file stream
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "logo.bmp", FileMode.Open);
            }
            else //if image not exists show image Nopoto.img
                return dt;

            br = new BinaryReader(fs);
            byte[] b = new byte[fs.Length];
            b = br.ReadBytes(Convert.ToInt32(fs.Length));
            row["Hinh"] = b;
            //row["HinhDetail"] = b;
            dt.Rows.Add(row);
            fs.Dispose();


            return dt;
        }

        
    }
}
